package com.yuyu.shop.application.service.tcc;

import com.yuyu.shop.api.apimodel.order.dto.OrderCreateDTO;
import com.yuyu.shop.domain.order.DO.OrderCreateDO;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.BusinessActionContextParameter;
import io.seata.rm.tcc.api.LocalTCC;
import io.seata.rm.tcc.api.TwoPhaseBusinessAction;

/**
 * @author
 * @version 1.0
 * @description: 订单组合服务(分布式事务)
 * @date 2022/12/12 10:21
 */
@LocalTCC
public interface OrderAssembleTccService {

    /**
     *一阶段, 用于资源的预警
     * @param  orderId  订单id
     * @param  goodsId  商品id
     * @param  stockNum 购买数量
     * @param businessActionContext 用于向二阶段传递参数,比如全局事务id, 分支id, 入参等
     * @return
     */
    @TwoPhaseBusinessAction(name = "orderTcc", commitMethod = "commit", rollbackMethod = "rollBack")
    OrderCreateDO tryCreate(BusinessActionContext businessActionContext,
                      @BusinessActionContextParameter(paramName ="orderType") Integer orderType,
                      @BusinessActionContextParameter(paramName = "shopId") Long shopId,
                      @BusinessActionContextParameter(paramName = "goodsId") Long goodsId,
                      @BusinessActionContextParameter(paramName = "stockNum") Integer stockNum,
                      @BusinessActionContextParameter(paramName = "orderId") String orderId);

    /**
     *二阶段commit方法,用于事务的提交
     * @param businessActionContext 用于获取一阶段的参数,比如全局事务id, 分支id, 入参等
     * @return
     */
    Boolean commit(BusinessActionContext businessActionContext);


    /**
     * 三阶段
     * @param businessActionContext 用于获取一阶段的参数,比如全局事务id, 分支id, 入参等
     * @return
     */
    Boolean rollBack(BusinessActionContext businessActionContext);
}
